package com.juc.threadPoolExecutor.threadPoolExecutor15.abortPolicy;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 
 * AbortPolicy:当任务添加到线程池中被拒绝时，它将抛出RejectedExecutionException 异常
 * CallerRunsPolicy:当任务添加到线程池中被拒绝时，会使用调用线程池的Thread
 * 线程对象处理被拒绝的任务
 * DiscardOldestPolicy:当任务添加到线程池中被拒绝时，线程池会放弃等待队列中
 * 最旧的未处理任务，然后将被拒绝的任务添加到等待队列中
 * DiscardPolicy:当任务添加到线程池中被拒绝时，线程池将丢弃被拒绝的任务
 */
public class Run {

	public static void main(String[] args) {
		/**
		 * AbortPolicy测试
		 */
		Runnable runnable =  new Runnable() {
			
			@Override
			public void run() {
			
				try {
					Thread.sleep(5000);
					System.out.println(Thread.currentThread().getName() + " run end !");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2), new ThreadPoolExecutor.AbortPolicy());
		executor.execute(runnable);//不报错
		executor.execute(runnable);//不报错
		executor.execute(runnable);//不报错
		executor.execute(runnable);//不报错
		executor.execute(runnable);//不报错
		//executor.execute(runnable);//报错
		/**
		 * 使用AbortPolicy策略后，线程数量超过max值后报错
		 * Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.juc.threadPoolExecutor.threadPoolExecutor15.abortPolicy.Run$1@5c647e05 rejected from java.util.concurrent.ThreadPoolExecutor@33909752[Running, pool size = 3, active threads = 3, queued tasks = 2, completed tasks = 0]

		 */
	}
}
